package com.sharer.last.service.impl;

import com.sharer.last.mapper.*;
import com.sharer.last.model.po.CityVisitorPO;
import com.sharer.last.model.po.VisitRecordPO;
import com.sharer.last.model.vo.category.CategoryBlogCountVO;
import com.sharer.last.model.vo.dashboard.CategoryForBlogCountVO;
import com.sharer.last.model.vo.dashboard.TagForBlogCountVO;
import com.sharer.last.model.vo.tag.TagBlogCountVO;
import com.sharer.last.service.BlogService;
import com.sharer.last.service.CategoryService;
import com.sharer.last.service.DashboardService;
import com.sharer.last.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 仪表盘业务层实现
 *
 * @ClassName DashboardServiceImpl
 * @Author wangjin
 * @Date 2023/5/5 20:44
 * @Description
 * @Version 1.0
 */
@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    BlogMapper blogMapper;

    @Resource
    TagMapper         tagMapper;
    @Resource
    VisitLogMapper    visitLogMapper;
    @Resource
    VisitRecordMapper visitRecordMapper;
    @Resource
    CityVisitorMapper cityVisitorMapper;

    @Resource
    BlogService blogService;

    @Resource
    CategoryService categoryService;

    @Resource
    TagService tagService;


    //查询最近30天的记录
    private static final int visitRecordLimitNum = 30;

    @Override
    public int countVisitLogByToday() {
        return visitLogMapper.countVisitLogByToday();
    }

    /**
     * 统计指定分类下的博客数量
     *
     * @return
     */
    @Override
    public CategoryForBlogCountVO getCategoryBlogCount() {
        //查询分类id对应的博客数量
        List<CategoryBlogCountVO> categoryBlogCountList = blogMapper.getCategoryBlogCountList();
        //所有分类名称的List
        List<String> legend = categoryBlogCountList.stream().map(CategoryBlogCountVO::getName).collect(Collectors.toList());
        return CategoryForBlogCountVO.builder().legend(legend).series(categoryBlogCountList).build();
    }

    /**
     * 统计指定标签下的博客数量
     *
     * @return
     */
    @Override
    public TagForBlogCountVO getTagBlogCount() {
        //查询标签id对应的博客数量
        List<TagBlogCountVO> tagBlogCountList = tagMapper.getTagBlogCount();
        // 获取所有标签名称
        List<String> legend = tagBlogCountList.stream().map(TagBlogCountVO::getName).collect(Collectors.toList());
        return TagForBlogCountVO.builder().legend(legend).series(tagBlogCountList).build();
    }

    @Override
    public Map<String, List> getVisitRecordMap() {
        List<VisitRecordPO> visitRecordList = visitRecordMapper.getVisitRecordListByLimit(visitRecordLimitNum);
        List<String>        date            = new ArrayList<>(visitRecordList.size());
        List<Integer>       pv              = new ArrayList<>(visitRecordList.size());
        List<Integer>       uv              = new ArrayList<>(visitRecordList.size());
        for (int i = visitRecordList.size() - 1; i >= 0; i--) {
            VisitRecordPO visitRecord = visitRecordList.get(i);
            date.add(visitRecord.getDate());
            pv.add(visitRecord.getPv());
            uv.add(visitRecord.getUv());
        }
        Map<String, List> map = new HashMap<>(8);
        map.put("date", date);
        map.put("pv", pv);
        map.put("uv", uv);
        return map;
    }

    @Override
    public List<CityVisitorPO> getCityVisitorList() {
        return cityVisitorMapper.getCityVisitorList();
    }
}
